using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;

public partial class MessageTypeEdt : BasePage
{
    SortedView messageTypesSorted;
    AdminServices.MessageTypeSearchRequest messageTypeSearchRequest;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ResetSortControls();
            SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, "");

            if (Request["Add"] != null)
                btnAddMessageType_Click(sender, e);
        }

    }

    public void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty, string commandName)
    {
        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        AdminServices.MessageType[] messageTypes;
        messageTypeSearchRequest = new AdminServices.MessageTypeSearchRequest();

        if (Request["MTName"] != null)        
            messageTypeSearchRequest.Description = Request["MTName"].ToString();

        if (Request["MTVersion"] != null)
            messageTypeSearchRequest.Version = Request["MTVersion"].ToString();

        messageTypes = proxy.MessageTypeSearch(CurrentPrincipal, messageTypeSearchRequest);  
        

        messageTypesSorted = new SortedView(messageTypes);

        if (sortProperty != "")
        {
            ListSortDirection lsd;
            if (sortDirection == "ASC")
                lsd = ListSortDirection.Ascending;
            else
                lsd = ListSortDirection.Descending;
            messageTypesSorted.ApplySort(sortProperty, lsd);
        }

        Session.Add("messageTypesSorted", messageTypesSorted);

        gvMessageType.DataSource = messageTypesSorted;
        gvMessageType.PageIndex = _pageIndex;

        if (commandName != "EditMT")
            gvMessageType.EditIndex = -1;

        gvMessageType.DataBind();
    }

    private void SearchAndBind(int _pageIndex, string sortDirection, string sortProperty)
    {
        SearchAndBind(_pageIndex, sortDirection, sortProperty, "");
    }

    protected void gvMessageType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        SearchAndBind(e.NewPageIndex, tbSortDirection.Text, tbSortProperty.Text);
        
        tbPage.Text = e.NewPageIndex.ToString();
    }

    protected void gvMessageType_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (tbSortDirection.Text == "ASC" && tbSortProperty.Text == e.SortExpression)
            tbSortDirection.Text = "DESC";
        else
            tbSortDirection.Text = "ASC";

        tbSortProperty.Text = e.SortExpression;
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, e.SortExpression);
    }

    private void ResetSortControls()
    {
        tbPage.Text = "0";
        tbSortDirection.Text = "ASC";
        tbSortProperty.Text = "";
    }

    protected void gvMessageType_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        AdminServices.AdminServices proxy = new AdminServices.AdminServices();
        messageTypesSorted = (SortedView)Session["messageTypesSorted"];

        //unlock all
        proxy.MessageTypeUnlockAll(CurrentPrincipal);

        switch (e.CommandName)
        {
            case "EditMT":
                //lock record
                proxy.MessageTypeLock(CurrentPrincipal, Convert.ToInt32(gvMessageType.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.MessageType)messageTypesSorted[int.Parse(e.CommandArgument.ToString())]).MessageTypeID);
                //turn grid to edit mode
                gvMessageType.EditIndex = int.Parse(e.CommandArgument.ToString());
                break;
            case "DeleteMT":
                //lock record
                proxy.MessageTypeLock(CurrentPrincipal, Convert.ToInt32(gvMessageType.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.MessageType)messageTypesSorted[int.Parse(e.CommandArgument.ToString())]).MessageTypeID);
                //delete record
                proxy.MessageTypeDelete(CurrentPrincipal, Convert.ToInt32(gvMessageType.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text)); //((AdminServices.MessageType)messageTypesSorted[int.Parse(e.CommandArgument.ToString())]).MessageTypeID);
                //unlock all
                proxy.MessageTypeUnlockAll(CurrentPrincipal);
                break;
            case "CancelMT":
                //Cancel Edit
                gvMessageType.EditIndex = -1;
                break;
        }
        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text, e.CommandName);
    }

    protected void btnAddMessageType_Click(object sender, EventArgs e)
    {
        messageTypesSorted = (SortedView)Session["MessageTypesSorted"];
        AdminServices.MessageType[] messageTypeNew = new AdminServices.MessageType[messageTypesSorted.Count + 1];
        messageTypesSorted.CopyTo(messageTypeNew, 1);

        AdminServices.MessageType messageType = new AdminServices.MessageType();
        messageType.MessageTypeID = -1;
        messageType.Description = "";
        messageTypeNew[0] = messageType;

        messageTypesSorted = new SortedView(messageTypeNew);

        //Session.Add("messageTypesSorted", messageTypesSorted);

        gvMessageType.EditIndex = 0;
        gvMessageType.PageIndex = 0;
        gvMessageType.DataSource = messageTypesSorted;
        gvMessageType.DataBind();

    }
    protected void btnSaveMessageType_Click(object sender, EventArgs e)
    {
        int messageTypeIndex = gvMessageType.EditIndex;
        if (messageTypeIndex > -1)
        {
            gvMessageType.UpdateRow(messageTypeIndex, false);
        }
    }

    protected void gvMessageType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells[0].Text == "-1")
        {
            e.Row.Cells[4].Controls[0].Visible = false; 
            e.Row.Cells[5].Controls[0].Visible = false;            
            e.Row.Cells[0].Text = "";           
        }
    }

    protected void gvMessageType_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string fID = Utilities.getCellValue((GridView)sender, e, 0, 0);
        string fDescription = Utilities.getCellValue((GridView)sender, e, 1, 1);
        string fXmlSchema = Utilities.getCellValue((GridView)sender, e, 2, 2);
        string fVersion = Utilities.getCellValue((GridView)sender, e, 3, 3);

        AdminServices.AdminServices proxy = new AdminServices.AdminServices();

        messageTypesSorted = (SortedView)Session["messageTypesSorted"];
        AdminServices.MessageType messageTypeEdit = new AdminServices.MessageType();

        if (fID == "")fID = "-1";
        messageTypeEdit.MessageTypeID = Convert.ToInt32(fID);
        messageTypeEdit.Description = fDescription;
        messageTypeEdit.Version = fVersion;
        messageTypeEdit.XmlSchema = fXmlSchema;

        if (messageTypeEdit.MessageTypeID > -1)
        {
            //Editted record
            proxy.MessageTypeUpdate(CurrentPrincipal, messageTypeEdit);
        }
        else
        {
            //insert record
            proxy.MessageTypeInsert(CurrentPrincipal, messageTypeEdit);
        }

        //unlock all
        proxy.MessageTypeUnlockAll(CurrentPrincipal);

        gvMessageType.EditIndex = -1;

        SearchAndBind(int.Parse(tbPage.Text), tbSortDirection.Text, tbSortProperty.Text);

    }

}
